const PythonWorker =(monaco)=>{
monaco.languages.register({
  id: 'python',
  extensions: ['.py'],
  aliases: ['Python', 'py'],
  mimetypes: ['text/x-python']
})
monaco.languages.setMonarchTokensProvider('python', {
  tokenizer: {
    root: [
      // 关键字高亮
      [
        /\b(?:import|from|def|class|return|if|elif|else|try|except|finally|with|for|while|break|continue|pass|yield|global|nonlocal|assert|del|lambda|is|in|and|or|not|True|False|None)\b/,
        'keyword'
      ],
      // 数字高亮
      [/\b\d+(\.\d+)?\b/, 'number'],
      // 字符串高亮
      [/(['"])(?:(?=(\\?))\2.)*?\1/, 'string'],
      // 注释高亮
      [/^\s*#.*$/, 'comment'],
      // 类名高亮
      [/\b[A-Z][\w]*\b/, 'type'],
      // 函数名高亮
      [/\bdef\s+(\w+)/, 'function'],
      // 变量名高亮（除了关键字之外）
      [/\b([a-zA-Z_]\w*)\b(?!\s*\()/, 'variable']
    ]
  }
})
monaco.languages.registerCompletionItemProvider('python', {
  provideCompletionItems: (model, position) => {
    const suggestions = [
      // --- 基本关键字 ---
      {
        label: 'import',
        kind: monaco.languages.CompletionItemKind.Keyword,
        insertText: 'import ',
        detail: 'Import a module'
      },
      {
        label: 'from',
        kind: monaco.languages.CompletionItemKind.Keyword,
        insertText: 'from ',
        detail: 'Import specific components'
      },
      {
        label: 'def',
        kind: monaco.languages.CompletionItemKind.Keyword,
        insertText: 'def ${1:function_name}(${2:args}):\n\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Define a function'
      },
      {
        label: 'class',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText:
          'class ${1:ClassName}(${2:object}):\n\tdef __init__(self, ${3:args}):\n\t\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Define a class'
      },
      {
        label: 'for',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'for ${1:item} in ${2:iterable}:\n\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'For loop'
      },
      {
        label: 'while',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'while ${1:condition}:\n\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'While loop'
      },
      {
        label: 'if',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'if ${1:condition}:\n\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'If statement'
      },
      {
        label: 'elif',
        kind: monaco.languages.CompletionItemKind.Keyword,
        insertText: 'elif ',
        detail: 'Else if statement'
      },
      {
        label: 'else',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'else:\n\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Else statement'
      },
      {
        label: 'try',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'try:\n\t$0\nexcept ${1:Exception} as ${2:e}:\n\tpass',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Try-except block'
      },
      {
        label: 'with',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText: 'with ${1:expression} as ${2:var}:\n\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'With statement'
      },
      // --- 内置函数 ---
      {
        label: 'print',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'print(${1:message})',
        detail: 'Print to console'
      },
      {
        label: 'len',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'len(${1:object})',
        detail: 'Get the length of an object'
      },
      {
        label: 'range',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'range(${1:stop})',
        detail: 'Generate a range of numbers'
      },
      {
        label: 'type',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'type(${1:object})',
        detail: 'Get the type of an object'
      },
      {
        label: 'str',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'str(${1:object})',
        detail: 'Convert to string'
      },
      {
        label: 'int',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'int(${1:object})',
        detail: 'Convert to integer'
      },
      {
        label: 'list',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'list(${1:iterable})',
        detail: 'Convert to list'
      },
      {
        label: 'dict',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'dict(${1:key_value_pairs})',
        detail: 'Create a dictionary'
      },
      {
        label: 'sorted',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'sorted(${1:iterable})',
        detail: 'Return a sorted list'
      },
      {
        label: 'open',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'open(${1:filename}, ${2:mode})',
        detail: 'Open a file'
      },
      {
        label: 'input',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'input(${1:prompt})',
        detail: 'Read input from user'
      },
      {
        label: 'map',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'map(${1:function}, ${2:iterable})',
        detail: 'Apply a function to all items in an iterable'
      },
      {
        label: 'filter',
        kind: monaco.languages.CompletionItemKind.Function,
        insertText: 'filter(${1:function}, ${2:iterable})',
        detail: 'Filter elements from an iterable'
      },
      // --- 字符串方法 ---
      {
        label: 'str.upper',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:string}.upper()',
        detail: 'Convert string to uppercase'
      },
      {
        label: 'str.lower',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:string}.lower()',
        detail: 'Convert string to lowercase'
      },
      {
        label: 'str.split',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:string}.split(${2:separator})',
        detail: 'Split string into list'
      },
      {
        label: 'str.join',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:separator}.join(${2:iterable})',
        detail: 'Join elements of an iterable'
      },
      {
        label: 'str.strip',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:string}.strip(${2:chars})',
        detail: 'Strip whitespace or characters'
      },
      // --- 列表方法 ---
      {
        label: 'list.append',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:list}.append(${2:item})',
        detail: 'Add item to list'
      },
      {
        label: 'list.extend',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:list}.extend(${2:iterable})',
        detail: 'Extend list with another iterable'
      },
      {
        label: 'list.pop',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:list}.pop(${2:index})',
        detail: 'Remove item at index'
      },
      {
        label: 'list.remove',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:list}.remove(${2:item})',
        detail: 'Remove first occurrence of value'
      },
      {
        label: 'list.index',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:list}.index(${2:item})',
        detail: 'Return index of item'
      },
      {
        label: 'list.sort',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:list}.sort()',
        detail: 'Sort list in place'
      },
      {
        label: 'list.reverse',
        kind: monaco.languages.CompletionItemKind.Method,
        insertText: '${1:list}.reverse()',
        detail: 'Reverse the list'
      },
      // --- 类模板 ---
      {
        label: 'class-template',
        kind: monaco.languages.CompletionItemKind.Snippet,
        insertText:
          'class ${1:ClassName}:\n\tdef __init__(self, ${2:args}):\n\t\t$0',
        insertTextRules:
          monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
        detail: 'Class definition template'
      }
    ]
    return { suggestions }
  }
})
}
export default PythonWorker

